<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**
         * @param {number} n
         * @return {boolean}
         */
        var isHappy = function (n) {
            var pre = n, cur = getNext(n)
            while (cur != 1 && pre != cur) {
                pre = getNext(pre)
                cur = getNext(getNext(cur))
            }
            return cur == 1
        };
        var getNext = function (n) {
            var s = 0
            while (n) {
                s += (n % 10) * (n % 10)
                n = Math.floor(n / 10)
            }
            return s
        }
        console.log(isHappy(19));
    </script>
</body>

</html>